/**
 * All rights Reserved, Designed By 信鸥科技
 * project : Innovation-design
 * Created by youquer on 17/11/15.
 * 注意：本内容仅限于信鸥科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 * Description:
 *
 */
import {
  postList, postAdd, postRemove, postUpdate, postInfo, postToUpdate,
} from '../services/news';
import { message } from 'antd';


export default {
  namespace: 'news',

  state: {
    dataList: [],

    loading: true,
    page: 1,
    total: 0,
    pageSize: 10,
    id: 0,

    //字段
    title: '', //标题
    cover: '', //封面图
    detail: '', //详情(没有)
    type: 0, //新闻类型
    content: '', //内容-富文本
    sort: 0, //排序
    appendix: '', //文件名

    //图片上传
    previewVisible: false,
    previewImage: '',
    fileList: [],
    fileLists: [],
  },

  effects: {
    /*** 新闻  ***/
    //列表
    *fetch({ payload, callback }, { call, put, select }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });

      var date = yield select(state => state.news);
      var params = {};
      if (payload){
        params = payload;
      }
      params["page"] = date.page;
      const data = yield call(postList, params);
      if (data.recode == 200) {
        yield put({
          type: 'save',
          payload: {dataList: data.body.newsList, total: data.body.total},
        });
      } else if (data.recode == 9000){
        // message.error("您的权限不足");
      } else if (data.recode == 1001){
        // window.location.hash = "/user/login"
      } else {
        // message.error('网络请求失败!');
      }

      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback();
    },
    //删除
    *remove({ payload, callback }, { call, put, select }) {
      var myState = yield select(state => state.news);
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const data = yield call(postRemove, payload);
      if (data){
        if (data.recode === 200){
          yield put({
            type: 'fetch',
            payload: {pageSize: myState.pageSize, title: myState.title, type: myState.type},
          })
        } else if (data.recode == 9000){
          // message.error("您的权限不足");
        } else if (data.recode == 1001){
          // window.location.hash = "/user/login"
        } else {
          // message.error('网络请求失败!');
        }
      } else {
        // message.error('网络连接失败');
      }
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback();
    },
    //添加
    *add({ payload }, { call ,put }) {
      yield put({
        type: 'changeLoading',
        payload: true
      });
      if (payload.title && payload.cover && payload.type && payload.content){
        const data = yield call(postAdd, payload);
        if (data.recode == 200){
          window.location.hash = "/news";
          yield put({
            type: "clear"
          });
        } else if (data.recode == 9000){
          // message.error("您的权限不足");
        } else if (data.recode == 1001){
          // window.location.hash = "/user/login"
        } else {
          // message.error('网络请求失败!');
        }
      } else {
        message.error("所有信息均为必填信息")
      }
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    //调到详情页
    *toUpdate({ callback, payload }, { call, put }){
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const data = yield call(postToUpdate, payload);
      if (data.recode == 200) {
        yield put({
          type: 'save',
          payload: data.body.news
        });
        yield put({
          type: 'save',
          payload: {fileList: [{
            uid: -1,
            name: data.body.news.cover,
            status: 'done',
            url: data.body.news.cover,
          }]}
        });
      } else if (data.recode == 9000){
        // message.error("您的权限不足");
      } else if (data.recode == 1001){
        // window.location.hash = "/user/login"
      } else {
        // message.error('网络请求失败!');
      }
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback();
    },
    //修改
    *update({ payload }, { call ,put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      if (payload.title && payload.cover && payload.type && payload.content){
        const data = yield call(postUpdate, payload);
        if (data.recode == 200){
          yield put({
            type: "clear"
          });
          window.location.hash = "/news" ;
        } else if (data.recode == 9000){
          // message.error("您的权限不足");
        } else if (data.recode == 1001){
          // window.location.hash = "/user/login"
        } else {
          // message.error('网络请求失败!');
        }
      } else {
        message.error("所有信息均为必填信息")
      }
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    //详情
    *info({ callback, payload }, { call, put }){
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const data = yield call(postInfo, payload);
      if (data.recode == 200) {
        yield put({
          type: 'save',
          payload: data.body.news
        });
      } else if (data.recode == 9000){
        // message.error("您的权限不足");
      } else if (data.recode == 1001){
        // window.location.hash = "/user/login"
      } else {
        // message.error('网络请求失败!');
      }
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback();
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    clear(state){
      return {
        ...state,
        ...{
          dataList: [],

          loading: true,
          page: 1,
          total: 0,
          pageSize: 10,
          id: 0,

          //字段
          title: '', //标题
          cover: '', //封面图
          detail: '', //详情(没有)
          content: '', //内容-富文本
          sort: 0, //排序
          appendix: '', //文件名

          //图片上传
          previewVisible: false,
          previewImage: '',
          fileList: [],
          fileLists: [],
        },
      };
    },
  },
};
